/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function initSideCategoryList(){
    $.ajax({
        type: "GET",
        url: "/public/blog/getAllCategories",
        data: {
            "task": "getAllCategories"
        },
        dataType: "json",
        success: function(json){
            updateSideCategoryList(json);
        }
    })
}

function updateSideCategoryList(json) {
    side_category_list = document.getElementById("category-list");
    category_list = document.createElement("ul");
    if(json.categories.length == 0){
        list_item = createLinkListItem("Sorry, No blog post now!", "#");
        category_list.appendChild(list_item);
    }else{
        for(index in json.categories) {
            category = json.categories[index];
            category_name = category.name;
            category_nickname = category.nickName;
            category_href = "/blog/category/"+category_nickname+"/1";

            list_item = createLinkListItem(category_name, category_href);
            category_list.appendChild(list_item);
        }
    }
    side_category_list.appendChild(category_list);
}

function initSideDateList(){
    category = null;
    $.ajax({
        type: "GET",
        url: "/public/blog/getAllDates",
        data: {
            "task": "getAllDates"
        },
        dataType: "json",
        success: function(json){
            updateSideDateList(json);
        }
    })

    return category;
}

function updateSideDateList(json) {
    side_date_list = document.getElementById("date-list");
    year_list = document.createElement("ul");
    if(json.length == 0){
        list_item = createLinkListItem("Sorry, No blog post now!", "#");
        category_list.appendChild(list_item);
    }else{
        for(year_name in json) {
            year = json[year_name];
            total_of_year = year.total;
            //year_href = "/blog/date/"+year_name+"/1";

            year_list_item = createLinkListItem(year_name+" ("+total_of_year+")", "#");
            $(year_list_item).toggle(
                function(){
                    $(this).children("ul").show();
                },
                function(){
                    $(this).children("ul").hide();
                });

            month_list = document.createElement("ul");
            month_list.setAttribute("class", "monthListForAsideDateList");
            
            for(month_name in year) {
                if(month_name != "total"){
                    month_href= "/blog/date/"+year_name+"/"+month_name+"/1";
                    month_list_item = createLinkListItem(month_name+" ("+year[month_name]+")", month_href)
                    month_list.appendChild(month_list_item);
                }

            }
            
            year_list_item.appendChild(month_list);
            year_list.appendChild(year_list_item);
        }
    }
    side_date_list.appendChild(year_list);
}

function createLinkListItem(name, href){
    link_item  = document.createElement("li");
    anchor = document.createElement("a");
    anchor.appendChild(document.createTextNode(name));
    anchor.setAttribute("href", href);
    anchor.setAttribute("title", "Display all blog belong to " + name);
    link_item.appendChild(anchor);

    return link_item;
}

$(document).ready(function(){
    initSideCategoryList();
    initSideDateList();
})
